// 导入Token合约
const Token = artifacts.require("Token")
// 导入周期性支付合约
const Payment = artifacts.require("Payment")

contract('Payment', (accounts) => {
    it('owner转账1000个AAA给用户1', async () => {
        // 拿到合约实例
        const tokenInstance = await Token.deployed();

        // 转账1000个AAA给用户1
        const amount = web3.utils.toWei("1000", "ether");
        await tokenInstance.transfer(
            accounts[1],
            amount
        );
        // 查询是否转账成功
        const balance = await tokenInstance.balanceOf(
            accounts[1],
        );

        // 判断是否符合预期
        assert.equal(balance, amount, "转账");
    });
    it('用户1授权1000个AAA给周期性支付合约', async () => {
        // 拿到合约实例
        const tokenInstance = await Token.deployed();
        const PaymentInstance = await Payment.deployed();

        // 用户1授权1000个AAA给周期性支付合约
        const amount = web3.utils.toWei("1000", "ether");
        await tokenInstance.approve(
            PaymentInstance.address,
            amount,
            {
                from: accounts[1]
            }
        );
        // 查询是否授权成功
        const allowance = await tokenInstance.allowance(
            accounts[1],
            PaymentInstance.address,
            {
                from: accounts[1]
            }
        );

        // 判断是否符合预期
        assert.equal(allowance, amount, "成功授权");
    });
    it('用户1发布周期性支付订单，接收人为用户2，数量1000 AAA，频率60秒/次，支付20次', async () => {
        // 拿到合约实例
        const tokenInstance = await Token.deployed();
        const PaymentInstance = await Payment.deployed();

        // 用户1发起周期性支付订单
        const amount = web3.utils.toWei("1000", "ether");
        await PaymentInstance.createPayment(
            tokenInstance.address,
            accounts[2],
            amount,
            Math.round(new Date().getTime() / 1000),
            60,
            0,
            20,
            { 
                from: accounts[1]
            }
        );
        // 查询订单是否发布成功
        const orders = await PaymentInstance.getFromPayments(accounts[1]);

        // 判断是否符合预期
        assert.equal(orders[0].amount, amount, "成功发布");
    });
    it('用户2修改接收人为用户3', async () => {
        // 拿到合约实例
        const PaymentInstance = await Payment.deployed();

        // 修改接收人为用户3
        await PaymentInstance.transferPayment(
            1,
            accounts[3],
            {
                from: accounts[2]
            }
        );
        // 查询是否修改成功
        const info = await PaymentInstance.getPaymentInfo(1);

        // 判断是否符合预期
        assert.equal(info.to, accounts[3], "成功修改");
    });

    // TODO 其他测试自己写...
});
